#pragma once

#define NOMINMAX
#include <windows.h>

#include <QImage>
#include <QMap>
#include <QString>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Metadata.h>
#include <winrt/Windows.Graphics.Capture.h>
#include <winrt/Windows.Graphics.DirectX.Direct3D11.h>
#include <d3d11.h>
#include <dxgi1_2.h>
#include <windows.graphics.capture.interop.h>
#include <windows.graphics.directx.direct3d11.interop.h>
#include <wrl/client.h>

#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "windowsapp.lib")

class WindowCapture
{
public:
    // 初始化主线程WinRT（STA）
    static bool initializeWinRT();
    
    // 初始化子线程WinRT（MTA）
    static bool initializeWinRTForThread();

    // 在主线程中创建GraphicsCaptureItem
    static winrt::Windows::Graphics::Capture::GraphicsCaptureItem createCaptureItemForWindow(HWND hwnd);

    // 在子线程中捕获（使用已创建的GraphicsCaptureItem）
    static QImage captureWithItem(winrt::Windows::Graphics::Capture::GraphicsCaptureItem item);

    // 捕获指定窗口（兼容接口）
    static QImage captureWindow(HWND hwnd, bool useWinRT = false);

    // 获取所有顶层窗口
    static QMap<HWND, QString> getAllWindows();

private:
    // WinAPI 捕获
    static QImage captureWindowWinAPI(HWND hwnd);

    // WinRT 捕获
    static QImage captureWindowWinRT(HWND hwnd);

    // WinRT 内部工具函数
    static winrt::Windows::Graphics::Capture::GraphicsCaptureItem createCaptureItem(HWND hwnd);
    static winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice createD3DDevice();
    static Microsoft::WRL::ComPtr<ID3D11Texture2D> getTextureFromSurface(
        winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DSurface const& surface);
};
